#define _CRT_SECURE_NO_WARNINGS 1

#include"Seqlist.h"


void SLInit(SL* ps)
{
	ps->arr = NULL;
 	ps->size = ps->capacity = 0;
}


void SLDestory(SL* ps)
{
	if (ps->arr)
		free(ps->arr);
	ps->arr = NULL;
	ps->capacity = ps->size = 0;
}


//void SLPirnt(SL* ps)
//{
//	assert(ps);
//	for (int i = 0; i < ps->size; i++)
//	{
//		printf("%d ", ps->arr[i]);
//	}
//	printf("\n");
//}


void SLCheakCapcity(SL* ps)
{
	assert(ps);
	if (ps->size == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDateType* tmp = (STDateType*)realloc(ps->arr, sizeof(STDateType) * newCapacity);
		if (tmp == NULL)
		{
			perror("realloc fail!\n");
			exit(1);
		}
		else
		{
			ps->arr = tmp;
			ps->capacity = newCapacity;
		}
	}
}


void SLPushBack(SL* ps, STDateType x)
{
	SLCheakCapcity(ps);
	ps->arr[ps->size] = x;
	ps->size++;
}


void SLPushFront(SL* ps, STDateType x)
{
	SLCheakCapcity(ps);
	for (int i = ps->size; i > 0; i--)
	{
		//ps->arr[1] = ps->arr[0];
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[0] = x;
	ps->size++;
}



void SLPopBack(SL* ps)
{
	assert(ps);
	assert(ps->size);
	ps->size--;
}


void SLPopFront(SL* ps)
{
	assert(ps);
	assert(ps->size);
	for (int i = 0; i < ps->size - 1; i++)
	{
		//ps->arr[size - 2] = ps->arr[size - 1];
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}


void SLInsert(SL* ps, int pos, STDateType x)
{
	assert(ps->size);
	assert(ps->size > pos && pos >= 0);
	SLCheakCapcity(ps);
	for (int i = ps->size; i > pos; i--)
	{
		//ps->arr[pos+1] = ps->arr[pos]
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[pos] = x;
	ps->size++;
}


void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(ps->size);
	assert(pos < ps->size && pos >= 0);
	for (int i = pos; i < ps->size - 1; i++)
	{
		//arr[size - 2] = arr[size - 1];
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}


//int SLFind(SL* ps, STDateType x)
//{
//	assert(ps);
//	for (int i = 0; i < ps->size; i++)
//	{
//		if (ps->arr[i] == x)
//		{
//			return i;
//		}
//	}
//	return -1;
//}
